package com.mt.skyhoist.ui.activity;

import static com.mt.skyhoist.ui.activity.ScanDeviceService.Refresh;
import static com.mt.skyhoist.ui.activity.ScanDeviceService.StartScan;

import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;


import com.mt.skyhoist.R;
import com.mt.skyhoist.util.GlobalVariable;
import com.mt.skyhoist.util.bean.DeviceModel;
import com.mt.skyhoist.util.view.DeviceChildView;
import com.mt.skyhoist.util.view.DeviceGroupView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;


public class HomeFragment extends Fragment {
	private GlobalVariable mGlobalVariable;
	private Logger log = LoggerFactory.getLogger("HomeFragment");
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mGlobalVariable = GlobalVariable.getinstance();
	}

	private View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (view != null) {
			return view;
		}

		view = inflater.inflate(R.layout.fragment_home, container, false);

		initView();

		return view;
	}

	// 初始化控件
	private ExpandableListView exlv;

	private void initView() {
		exlv = view.findViewById(R.id.exlv);

		exlv.setAdapter(adapter);
	}

	private BaseExpandableListAdapter adapter = new BaseExpandableListAdapter(){

		@Override
		public int getGroupCount() {
			return mGlobalVariable.getDeviceGroup().size();
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return mGlobalVariable.getDeviceGroup().get(groupPosition).size();
		}

		@Override
		public List<DeviceModel> getGroup(int groupPosition) {
			return mGlobalVariable.getDeviceGroup().get(groupPosition);
		}

		@Override
		public DeviceModel getChild(int groupPosition, int childPosition) {
			return mGlobalVariable.getDeviceGroup().get(groupPosition).get(childPosition);
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
			if (null == convertView) {
				convertView = new DeviceGroupView(HomeFragment.this.getContext());
			}

			((DeviceGroupView) convertView).setData(getGroup(groupPosition), isExpanded, groupPosition);
			((DeviceGroupView) convertView).setCallback(settingCallback);

			return convertView;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
			if (null == convertView) {
				convertView = new DeviceChildView(HomeFragment.this.getContext());
			}

			((DeviceChildView) convertView).setData(getChild(groupPosition, childPosition));

			return convertView;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return false;
		}
	};

	private DeviceGroupView.Callback settingCallback = new DeviceGroupView.Callback() {
		@Override
		public void onNameLongClick(int index) {
			System.out.println("Truth onNameLongClick->" + index);

			if (mGlobalVariable.getMixDatas().getSelectgrounp() == index){
				Toast.makeText(HomeFragment.this.getContext(), "Please disconnect station", Toast.LENGTH_LONG).show();
				return;
			}

			Intent intent = new Intent(HomeFragment.this.getActivity(), ScanActivity.class);
			intent.putExtra(ScanActivity.GROUNPINDEX, index);
			HomeFragment.this.getActivity().startActivityForResult(intent, index);
		}
	};

	@Override
	public void onResume() {
		super.onResume();
		EventBus.getDefault().register(this);

		EventBus.getDefault().post(StartScan);

		adapter.notifyDataSetChanged();
	}

	@Override
	public void onPause() {
		super.onPause();
		EventBus.getDefault().unregister(this);

		EventBus.getDefault().post(ScanDeviceService.StopScan);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onMessageEvent(Integer event) {
		switch (event) {
			case Refresh:
				adapter.notifyDataSetChanged();
				break;
		}
	}


}
